/*
 * #%L
 * SwiftUIWeb Maven Webapp
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;

import com.linkwithweb.products.swiftui.context.SystemContext;
import com.linkwithweb.products.swiftui.service.PlatformService;
import com.linkwithweb.products.swiftui.service.impl.ApprovalProcessWatcher;
import com.linkwithweb.products.swiftui.utils.FreeMarkerEngine;

/**
 * @author ashwin kumar
 * 
 */
public abstract class AbstractController extends AnnotationMethodHandlerAdapter{
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	@Qualifier("platformService")
	private PlatformService platformService;
	
	@Autowired
	@Qualifier("approvalProcessWatcher")
	protected ApprovalProcessWatcher approvalProcessWatcher;
	
	@Autowired
	@Qualifier("messageSource")
	private MessageSource messageSource;

	private List<String> systemColumnNames = new ArrayList<String>();

	/**
	 * @return
	 */
	public PlatformService getPlatformService() {
		return platformService;
	}

	/**
	 * @param paramName
	 * @return
	 */
	protected String getRequestParamter(String paramName) {
		String paramString = "${GlobalContext.requestContext.requestWrapper."
				+ paramName + "}";
		try {
			return FreeMarkerEngine.getInstance().evaluateString(paramString);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * @param paramName
	 * @return
	 */
	protected String getRequestParamterWithNoEvaluation(String paramName) {
		return SystemContext.getInstance().getRequestContext()
				.getRequestWrapper().getParameter(paramName);
	}

	/**
	 * @param columnName
	 * @return
	 */
	protected boolean isSystemColumn(String columnName) {
		if (systemColumnNames.isEmpty()) {
			systemColumnNames.add("IS_ACTIVE");
			systemColumnNames.add("IDENTIFIER");
			systemColumnNames.add("IDENTIFIER_ROLE");
			systemColumnNames.add("CREATED_BY");
			systemColumnNames.add("CREATED_DATE");
			systemColumnNames.add("MODIFIED_BY");
			systemColumnNames.add("MODIFIED_DATE");
			systemColumnNames.add("TRANSACTION_PAYLOAD");
		}
		for (String sysColumnName : systemColumnNames) {
			if (StringUtils.equalsIgnoreCase(columnName, sysColumnName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @param str
	 * @return
	 */
	protected String printCapitalized(String str) {
		str = str.replaceAll(" ", "").replaceAll("-", "").replaceAll("_", "");
		char ch; // One of the characters in str.
		char prevCh; // The character that comes before ch in the string.
		int i; // A position in str, from 0 to str.length()-1.
		StringBuffer sb = new StringBuffer();
		prevCh = '.'; // Prime the loop with any non-letter character.
		for (i = 0; i < str.length(); i++) {
			ch = str.charAt(i);
			if (Character.isLetter(ch) && !Character.isLetter(prevCh))
				sb.append(Character.toUpperCase(ch));
			else
				sb.append(ch);
			prevCh = ch; // prevCh for next iteration is ch.
		}

		return sb.toString();
	}

	/**
	 * @return
	 */
	protected String getUniqueString() {
		String returnString = "";
		Date newDate = new Date();
		try {

			SimpleDateFormat formatter;
			formatter = new SimpleDateFormat("yyyyMMddhhmm");
			java.util.Calendar cal = Calendar.getInstance(new SimpleTimeZone(0,
					"GMT"));
			formatter.setCalendar(cal);

			returnString = formatter.format(newDate);
			// System.out.println(formatter.format(newDate));
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}

		return returnString;
	}

	/**
	 * @return the messageSource
	 */
	public MessageSource getMessageSource() {
		return messageSource;
	}

	/**
	 * @param messageSource the messageSource to set
	 */
	public void setMessageSource(MessageSource messageSource) {
		this.messageSource = messageSource;
	}
	
	

}
